/******************************************************************************
@file:  SellProduct.java
@author Ramesh Anandhan

DESCRIPTION
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
 ******************************************************************************/
package com.smartc.trading;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.smartc.connection.HttpConnection;
import com.smartc.connection.ParsedLoginData;
import com.smartc.ui.AccountLogin;
import com.smartc.ui.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SellProduct extends Activity implements OnClickListener {

	protected static final String TAG = null;
	private static final int DIALOG_STATUS = 0;
	private final int PROGRESS_DLG = 0x01;
	private EditText mEditName = null;
	private EditText mEditDesc = null;
	private EditText mEditPrice = null;
	private EditText mEditTime = null;

	private Button mBtnSell = null;

	private HttpConnection mCityConnection;
	private ResponseHandler mResponse = new ResponseHandler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sell_product);

		mEditName = (EditText) findViewById(R.id.sell_edit_name);
		mEditDesc = (EditText) findViewById(R.id.sell_edit_desc);
		mEditPrice = (EditText) findViewById(R.id.sell_edit_price);
		mEditTime = (EditText) findViewById(R.id.sell_edit_hours);

		mBtnSell = (Button) findViewById(R.id.sell_btn);
		mBtnSell.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		if (view.getId() == R.id.sell_btn) {
			if(validateInput() == false)
				return;
			String name = mEditName.getText().toString().trim();
			String desc = mEditDesc.getText().toString().trim();
			String price = mEditPrice.getText().toString().trim();
			String hours = mEditTime.getText().toString().trim();

			SimpleDateFormat format = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			Calendar cal = Calendar.getInstance();
			Date date = cal.getTime();
			String currentTime = format.format(date);
			if (AccountLogin.DEBUG)
				Log.i(TAG, "onClick currentTime:" + currentTime);
			if (AccountLogin.DEBUG)
				Log.i(TAG, "onClick name:" + name);
			if (AccountLogin.DEBUG)
				Log.i(TAG, "onClick desc:" + desc);
			if (AccountLogin.DEBUG)
				Log.i(TAG, "onClick price:" + price);
			if (AccountLogin.DEBUG)
				Log.i(TAG, "onClick hours:" + hours);
			String domain = getResources().getString(R.string.domain);
			String query = getResources().getString(
					R.string.url_trade_sell_product);
			String url = domain + query;
			if (AccountLogin.DEBUG)
				Log.i(TAG, "onClick url:" + url);
			// ParsedLoginData.mIdUser = "1";//TO_DO
			mCityConnection = new HttpConnection();
			mCityConnection.setUrl(url);
			mCityConnection.clearParam();
			mCityConnection.AddParam("nmTradeProduct", name);
		    mCityConnection.AddParam("txtTradeProduct", desc);
			mCityConnection.AddParam("nrTradeProductPrice", price);
			mCityConnection.AddParam("idUser", ParsedLoginData.mIdUser);
			mCityConnection.AddParam("dtTradeStart", currentTime);
			mCityConnection.AddParam("nrMaxHour", hours);
			mCityConnection.AddHandler(mResponse);
			mCityConnection.Execute();
			showDialog(PROGRESS_DLG);
		}
	}
	
	private boolean validateInput(){
		boolean valid = true;
		if(mEditName.getText().toString().trim().length() <= 0 ||
				mEditDesc.getText().toString().trim().length() <= 0 ||
				mEditPrice.getText().toString().trim().length() <= 0 ||
				mEditTime.getText().toString().trim().length() <= 0){
			valid = false;
			Toast.makeText(this, "Enter All Input fields", Toast.LENGTH_SHORT)
					.show();
		}
		return valid;
	}

	public class ResponseHandler extends DefaultHandler {

		//private boolean Messages = false;
		private boolean status = false;
		private boolean message = false;

		public String mStatus = null;
		public String mMessage = null;

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (this.status) {
				mStatus = new String(ch, start, length);
			} else if (this.message) {
				mMessage = new String(ch, start, length);
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if (localName.equals("Messages")) {
				//this.Messages = false;
				Message myMessage = new Message();
				myMessage.obj = "STATUS";
				handler.sendMessage(myMessage);
			} else if (localName.equals("status")) {
				this.status = false;
			} else if (localName.equals("message")) {
				this.message = false;
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (localName.equals("Messages")) {
				//this.Messages = true;
			} else if (localName.equals("status")) {
				this.status = true;
			} else if (localName.equals("message")) {
				this.message = true;
			}
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage msg:" + (String) msg.obj);
			String loginmsg = (String) msg.obj;
			if (loginmsg != null && loginmsg.equals("STATUS")) {
				removeDialog(PROGRESS_DLG);
				showDialog(DIALOG_STATUS);
			}

		}
	};

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {

		switch (id) {
		case DIALOG_STATUS:
			return new AlertDialog.Builder(SellProduct.this).setMessage(
					mResponse.mMessage).setPositiveButton(
					getResources().getString(R.string.ok), null).create();
		case PROGRESS_DLG:
			ProgressDialog mDialog = new ProgressDialog(this);
			mDialog.setMessage("Request In progress....");
			mDialog.setIndeterminate(true);
			mDialog.setCancelable(true);
			return mDialog;
		default:
			return super.onCreateDialog(id, args);
		}
	}
}
